package com.yowasa.xiuxian.engine.message.user;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.service.RedisService;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;


@CMD(name = "/个人信息", scope = CmdScopeEnum.ALL, outCmd = true)
public class ShowMeHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ExtendHelper extendHelper;
    @Autowired
    private RedisService redisService;


    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        StringBuilder sb = new StringBuilder();
        sb.append(userBO.getDesc());
        XiuXianExtend byAccountId = extendHelper.getByAccountId(userBO.getId());
        sb.append("\n").append(byAccountId.toString());
        String join = String.join(",", userBO.getUserConfig().getStudiedSpecialEffect());
        sb.append("\n学会的神通：\n").append(join);
        if (userBO.getLockEndTime().compareTo(new Date()) > 0) {
            sb.append("\n").append(userBO.getLockReason());
        }
        sb.append("\n当前所处的地点为："+userBO.getLocalName());
//        try {
//            String redisKey = "mys:xiuxian:userInfo:imageUrl:"+userBO.getUserId();
//            String imageUrl = (String) redisService.get(redisKey);
//            if(StrUtil.isBlank(imageUrl)){
//                Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
//                // 设置模板文件所在的路径
//                configuration.setDirectoryForTemplateLoading( ResourceUtils.getFile("classpath:image/html"));
//                // 加载模板文件
//                Template template = configuration.getTemplate("template.html","UTF-8");
//                // 创建一个数据模型
//                Map<String, Object> dataModel = new HashMap<>();
//                JSONObject jsonObject = JSONUtil.parseObj(message.getContent());
//                String imgaeUrl = (String) jsonObject.getJSONObject("user").get("portrait");
//                String mental = StringUtils.isBlank(userBO.getUserConfig().getMental()) ? "无" : userBO.getUserConfig().getMental();
//                String lingGen = ElementEnum.getLingGenDesc(userBO.getElement(), XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY);
//                String levelName = StageEnum.getById(userBO.getStage()).getName();
//                dataModel.put("user", userBO);
//                dataModel.put("userExtend", byAccountId);
//                dataModel.put("imageUrl", imgaeUrl);
//                dataModel.put("mental", mental);
//                dataModel.put("lingGen", lingGen);
//                dataModel.put("levelName", levelName);
//                dataModel.put("specialEffect", join);
//                if (userBO.getLockEndTime().compareTo(new Date()) > 0) {
//                    dataModel.put("lockEndTime", userBO.getLockReason());
//                } else {
//                    dataModel.put("lockEndTime", "当前处于空闲状态");
//                }
//                // 渲染模板并将结果输出到字符串中
//                StringWriter writer = new StringWriter();
//                template.process(dataModel, writer);
//                long time = new Date().getTime();
//                File file = ResourceUtils.getFile("classpath:image/gen/");
//                String absolutePath = file.getAbsolutePath() + "/";
//                FileOutputStream fileOutputStream = new FileOutputStream(absolutePath + time + ".html");
//                fileOutputStream.write(writer.toString().getBytes());
//                PagescreenshotUtil.screenShotWx(absolutePath + time + ".html", "C:\\opt\\pic\\mysPic\\" + time + ".jpg");
//                String url = "http://121.4.252.124:8080/images/pic/mysPic/" + time + ".jpg";
//                String s = messageHelper.tranferImage(message, url);
//                JSONObject tranferObj = JSONUtil.parseObj(s);
//                Integer retcode = tranferObj.getInt("retcode");
//                if(retcode != 0){
//                    StringBuffer sb2 = new StringBuffer();
//                    sb2.append(tranferObj.getStr("message")).append("\n图片生成失败，请联系别野房东，将机器人移除后重新添加即可\n\n").append(sb.toString());
//                    messageHelper.replyMys(message,sb2.toString());
//                    return;
//                }
//                //转存成功，存redis
//                imageUrl = tranferObj.getJSONObject("data").getStr("new_url");
//                redisService.set(redisKey,imageUrl);
//            }
//            messageHelper.imageSendMys(message, imageUrl);
//        } catch (Exception e) {
//            throw new HandlerException(e.toString());
//        }
        messageHelper.replyMys(message, sb.toString());
    }

    public String desc() {
        return new StringBuffer().append("查看自己的个人面板\n")
                .toString();

    }
}
